package com.huawen.cms.app.controller;

import com.alibaba.fastjson.JSON;
import com.huawen.cms.app.dto.CategoryTree;
import com.huawen.cms.app.dto.WebConfigTree;
import com.huawen.cms.app.entity.Article;
import com.huawen.cms.app.entity.ArticlePart;
import com.huawen.cms.app.entity.WebConfig;
import com.huawen.cms.app.mapper.WebConfigMapper;
import com.huawen.cms.app.service.ArticlePartService;
import com.huawen.cms.app.service.ArticleService;
import com.huawen.cms.app.service.CategoryService;
import com.huawen.cms.app.service.WebConfigService;
import com.huawen.cms.app.util.ConstantEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("web")
@Controller
public class WebController extends WebBaseController {

    @Autowired
    private WebConfigService webConfigService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticlePartService articlePartService;


    // region 首页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Map<String, Object> map) {

        initHeaderAndFooter(map);

        initBannerList(map);

        initCaseEntry(map);

        initNewsEntry(map);

        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "/indexJson", method = RequestMethod.GET)
    public String indexJson(Map<String, Object> map) {

        initHeaderAndFooter(map);

        initBannerList(map);

        initCaseEntry(map);

        initNewsEntry(map);

        String json = JSON.toJSONString(map);

        return json;
    }

    // endregion

    // region  业务  右侧文章
    @RequestMapping(value = "/product/{categoryId}", method = RequestMethod.GET)
    public String product(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {
        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        //右侧导航tree
        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);


        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                showArtId = item.getArtId();
                break;
            }
        }

        initArt(showArtId, map);

        return "yewu";
    }

    @ResponseBody
    @RequestMapping(value = "/yewuJson/{categoryId}", method = RequestMethod.GET)
    public String yewuJson(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {

        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);

        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                break;
            }
        }

        initArt(showArtId, map);


        String json = JSON.toJSONString(map);

        return json;
    }

    //endregion

    // region  案例  右侧列表
    @RequestMapping(value = "/product_list/{categoryId}", method = RequestMethod.GET)
    public String productList(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {
        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        //右侧导航tree
        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);


        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                showArtId = item.getArtId();
                break;
            }
        }

        initArt(showArtId, map);


        Article filter=new Article();
        filter.setDataFlag(1);
        filter.setCategoryId(subId);
        filter.setArtType(0); //不是直接介绍
        List<Article> artList=articleService.getList(filter);
        map.put("CaseArtList", artList);

        return "product_list";
    }

    @ResponseBody
    @RequestMapping(value = "/productListJson/{categoryId}", method = RequestMethod.GET)
    public String productListJson(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {

        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);

        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                break;
            }
        }

        initArt(showArtId, map);

        String json = JSON.toJSONString(map);

        return json;
    }

    //endregion

    // region  案例  右侧列表
    @RequestMapping(value = "/product_detail/{categoryId}", method = RequestMethod.GET)
    public String productDetail(@PathVariable("categoryId") Integer categoryId, Integer subId, Integer artId, Map<String, Object> map) {
        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        //右侧导航tree
        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);


        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            //showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                showArtId = item.getArtId();
                break;
            }
        }



        initArt(artId, map);

        return "product_detail";
    }

    @ResponseBody
    @RequestMapping(value = "/productDetailJson/{categoryId}", method = RequestMethod.GET)
    public String productDetailJson(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {

        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);

        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                break;
            }
        }

        initArt(showArtId, map);


        Article filter=new Article();
        filter.setDataFlag(1);
        filter.setCategoryId(subId);
        List<Article> artList=articleService.getList(filter);
        map.put("CaseArtList", tree);


        String json = JSON.toJSONString(map);

        return json;
    }

    //endregion



    // region  案例  右侧列表
    @RequestMapping(value = "/yewu/{categoryId}", method = RequestMethod.GET)
    public String yewu(@PathVariable("categoryId") Integer categoryId, Integer subId, Map<String, Object> map) {
        initHeaderAndFooter(map);
        initYeWuBannerList(map);

        //右侧导航tree
        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);


        Integer showArtId = 0;
        //右侧导航第一个  如果没传就取一个
        if (subId == null) {
            CategoryTree firstNavCategory = tree.getChildren().get(0);
            subId = firstNavCategory.getId();
            showArtId = firstNavCategory.getArtId();
        }

        // 如果传了 subId 就以subId为准  否则取第一个
        for (CategoryTree item :
                tree.getChildren()) {
            if (item.getId() == subId) {
                item.setActived(true);
                showArtId = item.getArtId();
                break;
            }
        }

        initArt(showArtId, map);

        return "yewu";
    }

    //endregion

    //region 业务页
    public void initArt(Integer artId, Map<String, Object> map) {

        Article art = (Article) articleService.getById(artId);
        map.put("ArtEntity", art);

        ArticlePart filter = new ArticlePart();
        filter.setArtId(artId);
        filter.setDataFlag(1);
        List<ArticlePart> artPartList = articlePartService.getList(filter);

        map.put("ArtPartList", artPartList);

    }

    public void initNavCategoryTree(Integer categoryId, Map<String, Object> map) {
        CategoryTree tree = categoryService.getTree(categoryId, 1);
        map.put("NavCategoryTree", tree);
    }

    // endregion

    public void initBannerList(Map<String, Object> map) {
        WebConfig webConfigReq = new WebConfig();
        webConfigReq.setParentId(ConstantEnum.HomeBannerList);
        List<WebConfig> bannerList = webConfigService.getList(webConfigReq);

        map.put("BannerList", bannerList);
    }

    public void initYeWuBannerList(Map<String, Object> map) {
        WebConfig webConfigReq = new WebConfig();
        webConfigReq.setParentId(ConstantEnum.YeWuBannerEntryId);
        List<WebConfig> bannerList = webConfigService.getList(webConfigReq);
        map.put("YeWuBannerList", bannerList);
    }


    public void initCaseEntry(Map<String, Object> map) {
        WebConfig webConfigReq = new WebConfig();
        webConfigReq.setParentId(ConstantEnum.HomeCaseEntryId);
        List<WebConfig> bannerList = webConfigService.getList(webConfigReq);

        map.put("CaseList", bannerList);
    }

    public void initNewsEntry(Map<String, Object> map) {
        WebConfig webConfigReq = new WebConfig();
        webConfigReq.setParentId(ConstantEnum.HomeBottomEntryId);
        List<WebConfig> bannerList = webConfigService.getList(webConfigReq);
        map.put("NewsList", bannerList);
    }

    //region 头部辅助

    public void initHeaderAndFooter(Map<String, Object> map) {
        //头部专用
        //头部专用
        List<WebConfigTree> tree = getHeaderFrontConfigTree();
        map.put("HeaderEntity", tree);

        //底部友链
        WebConfig friendLinkListReq = new WebConfig();
        friendLinkListReq.setParentId(ConstantEnum.FootrerFriendLink);
        List<WebConfig> friendLinkList = webConfigService.getList(friendLinkListReq);
        map.put("FriendLinkList", friendLinkList);

    }

    public List<WebConfigTree> getHeaderFrontConfigTree() {

        List<WebConfigTree> headerFrontConfigTreeList = new ArrayList<>();

        WebConfig bannerListReq = new WebConfig();
        bannerListReq.setParentId(ConstantEnum.HomeNavId);//首页导航
        bannerListReq.setDataFlag(1);
        List<WebConfig> headerPageConfigList = webConfigService.getList(bannerListReq);

        if (headerPageConfigList == null || headerPageConfigList.size() == 0) {
            return headerFrontConfigTreeList;
        }

        headerFrontConfigTreeList = ConverToTree(headerPageConfigList);

        for (WebConfigTree tree : headerFrontConfigTreeList) {

            WebConfig curReq = new WebConfig();
            curReq.setParentId(tree.getId());
            List<WebConfig> childrenList = webConfigService.getList(curReq);
            if (childrenList != null && childrenList.size() > 0) {
                tree.setChildren(ConverToTree(childrenList));
            }
        }

        return headerFrontConfigTreeList;
    }

    public List<WebConfigTree> ConverToTree(List<WebConfig> list) {
        List<WebConfigTree> tree = new ArrayList<>();
        for (WebConfig item : list) {
            WebConfigTree curTree = new WebConfigTree();

            curTree.setId(item.getId());
            curTree.setName(item.getName());
            curTree.setLevel(item.getLevel());
            curTree.setImgUrl(item.getImgUrl());
            curTree.setTitle(item.getTitle());
            curTree.setToUrl(item.getToUrl());
            curTree.setOrderNum(item.getOrderNum());
            curTree.setRemark(item.getRemark());
            curTree.setDataFlag(item.getDataFlag());
            tree.add(curTree);
        }
        return tree;

    }

    //endregion
}
